/*
 * @Description: 请填写简介
 * @Author: error: git config user.name & please set dead value or install git
 * @Date: 2022-05-30 14:09:31
 * @LastEditors: 15694822360
 * @LastEditTime: 2022-06-02 15:56:41
 * 
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
//import Home from '../views/Home.vue'
import Main from '../views/main.vue'
import Register from '../views/register'
import Scene from '../views/scene.vue'
import Viewpic from '../views/menu/viewpic/viewpic.vue'
//import List1 from '../views/menu/viewpic/list1.vue'
import CardList from '../views/main/cardList.vue'
import Mainimg from '../views/main/mainimg.vue'
import Index from "../views/menu/index/index.vue"
import user from "../views/menu/viewpic/user/user.vue"
Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    {
        path: '/main', name: "Main", component: Main, redirect: '/main/index', meta: {
            requireAuth: true
        },
        children: [
            {
                path: 'index', name: "index", component: Index,
            },
            {
                path: 'viewpic', name: "viewpic", component: Viewpic,
            },
            // {
            //     path: 'list1', name: "list1", component: List1
            // },
            {path:'user',name:'user',component:user},
            {
                path: 'cardList', name: "cardList", component: CardList
            },
            {
                path: 'mainimg', name: "mainimg", component: Mainimg
            }]
    },
    { path: '/scene', name: "Scene", component: Scene }

]


const router = new VueRouter({
    routes
})

router.beforeEach((to,from,next)=>{
    //判断该路由是否需要登录权限
    console.log(to.matched.some(record => record.meta.requireAuth));
    //record.meta.requireAuth是获取到该请求中携带的该参数
    if (to.matched.some(record => record.meta.requireAuth)){
        //获取到本地的token
        const token = localStorage.getItem("token")
        console.log("显示token----------：" + token)
        //判断当前的token是否存在，也就是登录时的token
        if (token){
            //如果指向的是登录页面，不做任何操作
            // eslint-disable-next-line no-empty
            if (to.path === "/login"){

            }else {
                //如果不是登录页面，且token存在，就放行
                next()
            }
        }else {
        //    如果token不存在
        //    前往登录
            next({path:'/login'})
        }

    }else {
        //如果不需要登录认证，就直接访问
        next()
    }
})


export default router